<template>
  <div>
    <BasicTable
      @register="registerTable"
      title="危险源导则库管理"
      :pagination="false"
      :scroll="{ x: false, y: 'calc(100vh - 300px)' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="onAddTemplateHazardButtonClick">新增</a-button>
      </template>
      <template #hdbnm="{ column, record }">
        <span
          v-if="record.fileid != null && record.fileid !== 0"
          @click="onDownloadPdfButtonClick(record)"
          >{{ record.hdbnm }}</span
        >
        <span v-else>{{ record.hdbnm }}</span>
      </template>
      <template #isconstruction="{ column, record }">
        <template v-if="record.isconstruction">
          <Tag :color="'green'">是</Tag>
        </template>
        <template v-else>
          <Tag :color="'red'">否</Tag>
        </template>
      </template>
      <template #action="{ column, record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditTemplateHazardButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: onDeleteTemplateHazardButtonClick.bind(null, record),
              },
            },
            {
              icon: 'ant-design:file-text-outlined',
              label: '危险源清单',
              onClick: onViewHazardsListButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:file-pdf-outlined',
              label: '下载PDF',
              ifShow: record.fileid != null && record.fileid !== 0,
              onClick: onDownloadPdfButtonClick.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddOrUpdateTemplateHazard
      @register="registerAddOrUpdateTemplateHazardModal"
      @success="afterSuccess"
    />
    <ViewHazardsList @register="registerViewHazardsListModal" @success="afterSuccess" />
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref, onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  import { Tag } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { queryDangerDb, deleteDangerDb } from '/@/api/ehs/templateHazard';
  import { TplHazardDbDto } from '/@/api/ehs/model/templateHazardModel';
  import { useDrawer } from '/@/components/Drawer';
  import {
    templateHazardTableColumns,
    queryHazardDbConditionFormSchema,
  } from './templateHazard.data';
  import AddOrUpdateTemplateHazard from './AddOrUpdateTemplateHazard.vue';
  import ViewHazardsList from './ViewHazardsList.vue';
  import { queryEnumOptions } from '/@/api/platform/enum';
  import { useGlobSetting } from '/@/hooks/setting';
  import { getPdfFileApi } from '/@/api/platform/file';

  export default defineComponent({
    name: 'templateHazardIndex',
    components: {
      BasicTable,
      TableAction,
      Tag,
      AddOrUpdateTemplateHazard,
      ViewHazardsList,
    },
    setup() {
      const { uploadUrl } = useGlobSetting();
      const [registerAddOrUpdateTemplateHazardModal, { openModal: openAddOrUpdateTemplateHazard }] =
        useModal();
      const [registerViewHazardsListModal, { openDrawer: openViewHazardsList }] = useDrawer();
      const [registerTable, { reload, getForm }] = useTable({
        api: queryDangerDb,
        columns: templateHazardTableColumns,
        canResize: true,
        striped: true,
        showTableSetting: true,
        showIndexColumn: true,
        isTreeTable: true,
        bordered: true,
        ellipsis: true,
        indexColumnProps: {
          width: 50,
        },
        useSearchForm: true,
        formConfig: {
          labelWidth: 100,
          schemas: queryHazardDbConditionFormSchema,
          autoSubmitOnEnter: true,
        },
        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });
      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }
      /**
       * 打开新增窗口
       */

      function onAddTemplateHazardButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '危险源导则库管理',
          operateType: OperationTypeEnum.ADD,
        };
        openAddOrUpdateTemplateHazard(true, propsData);
      }
      /**
       * 打开编辑窗口
       * @param record
       */
      function onEditTemplateHazardButtonClick(record: TplHazardDbDto) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '危险源导则库管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.hdbcd,
        };
        openAddOrUpdateTemplateHazard(true, propsData);
      }

      /**
       * 删除
       * @param record
       */
      function onDeleteTemplateHazardButtonClick(record: TplHazardDbDto) {
        deleteDangerDb(record.hdbcd)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      function onDownloadPdfButtonClick(record) {
        window.open(uploadUrl + getPdfFileApi + record.fileid, '_blank');
      }

      function onViewHazardsListButtonClick(record: TplHazardDbDto) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '危险源清单',
          operateType: OperationTypeEnum.VIEW,
          dataId: record.hdbcd,
          dataTitle: record.hdbnm,
        };
        openViewHazardsList(true, propsData);
      }
      onMounted(() => {
        queryEnumOptions('EhsHazardKeyTypeEnum').then((res) => {
          // console.log('option res', res);
          if (res != undefined && res != null && res.length > 0x0) {
            const value = res[0].options.map((option) => {
              return {
                value: Number(option.key),
                label: option.value,
              };
            });
            getForm().updateSchema({
              field: 'keytype',
              componentProps: {
                options: value,
              },
            });
          }
        });
        queryEnumOptions('EhsHazardCategoryTypeEnum').then((res) => {
          // console.log('option res', res);
          if (res != undefined && res != null && res.length > 0x0) {
            const value = res[0].options.map((option) => {
              return {
                value: Number(option.key),
                label: option.value,
              };
            });
            getForm().updateSchema({
              field: 'tags',
              componentProps: {
                options: value,
              },
            });
          }
        });
      });
      return {
        registerAddOrUpdateTemplateHazardModal,
        registerTable,
        registerViewHazardsListModal,
        afterSuccess,
        onAddTemplateHazardButtonClick,
        onEditTemplateHazardButtonClick,
        onDeleteTemplateHazardButtonClick,
        onViewHazardsListButtonClick,
        onDownloadPdfButtonClick,
      };
    },
  });
</script>

<style scoped></style>
